#include "Tree.h"
#include "Environment.h"
#include <cmath>



Tree::Tree(EcoSystem *eco_system) :
Producer(eco_system)
{
    set_age(0);
    //!!!!!!!!!!!!!!!!!!!!
    set_max_hp(1500);
    set_hp(1200);
    //!!!!!!!!!!!!!!!!!!!!
}
Gdiplus::Image *Tree::get_entity_image() const
{
    static Gdiplus::Image *img = Gdiplus::Image::FromFile(L".\\images\\tree.png");
    static Gdiplus::Image *img1 = Gdiplus::Image::FromFile(L".\\images\\emptytree.png");;
    if (get_hp() / get_max_hp() > 0.5)
        return img;
    else
        return img1;
}

Entity *Tree::new_entity() const
{
    return new Tree(get_eco_system_instance());
}

const unsigned int Tree::get_species_id() const
{
    return TREE_SPECIES_ID;
}


void Tree::on_tick()
{

    set_age(get_age() + 1);

    Environment *ev_ptr = eco_system->get_environment_instance();
    int temperature = ev_ptr->get_temperature(), brightness = ev_ptr->get_brightness();
    if (brightness > 0.6 && (temperature > 16 && temperature < 30))
    {
        if (get_age()<500)
            set_max_hp(get_max_hp()*(1 + 0.5 / (1 + exp(get_age() / 10))));
        set_hp(min(get_hp()*1.02, get_max_hp()));
    }
    else if (temperature < 5 || temperature > 35)
    {
        set_hp(get_hp()-50);
    }

    if (get_age() > LIFE_SPAN || get_hp() < 100)
        set_dead();
}

int Tree::get_cost_of_being_preyed() const
{
    return 0;
}

int Tree::get_gain_after_being_preyed() const
{
    return  0.015*get_hp();
}